#include "cgetoncar.h"
#include <QDataStream>
#include <QTcpSocket>
#include "protocol/GetOnCar.h"
#include "protocol/srvGetOnCarAns.h"

CGetOnCar::CGetOnCar(QObject *parent)
{
}

CGetOnCar::~CGetOnCar()
{
}

bool CGetOnCar::prepareReadData(QTcpSocket *pTcpSocket, protocal::BasePtc *basePtc)
{
    // just tell the client that the server has got the get_on_car message
    QDataStream in(pTcpSocket);

    // follow the criteria of srvGetOnCarAns.h
    if (   basePtc->source != protocal::SERVER\
        || basePtc->srv_command != protocal::SRV_GET_CLIENT_ON_MESSAGE
        || basePtc->size != sizeof(protocal::SrvGetOnCarAns))
    {
        // TODO: check whether it is necessary to clear the contents of in or pTcpSocket.
        // make an experiment to verify it.
        return false;
    }

    return true;
}

void CGetOnCar::prepareWriteData(QByteArray *block, void *param)
{
    // we cannot discrete this definition to the outside of the block,
    // and pass the reference to the funtion, or it will cause some
    // unknown problems, such as wrong streams. Maybe it is a bug of QT.
    QDataStream out(block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_3);


    protocal::GetOnCar* pGetOnCar = (protocal::GetOnCar*)param;

    // output the head information
    out << (qint32)(pGetOnCar->source) \
        << (qint32)(pGetOnCar->cli_command) \
        << (qint32)(pGetOnCar->size);

    char * temp = new char [10];
    memcpy(temp, pGetOnCar->orderid, 10);
    out << temp;
    delete[] temp;

    // send the current position
    out << pGetOnCar->gps.lat << pGetOnCar->gps.lon << pGetOnCar->gps.time;

    // send current time
    out << pGetOnCar->hour << pGetOnCar->minute << pGetOnCar->second;

    // send current date
    out << pGetOnCar->year << pGetOnCar->month << pGetOnCar->day;
}

void CGetOnCar::formResult()
{
    // do nothing for no data will be sent in
}
